//
//  HRJobDetailViewController.m
//  Btpin
//
//  Created by Vic on 2019/3/23.
//  Copyright © 2019 Vic. All rights reserved.
//

#import "HRJobDetailViewController.h"
#import "CompanyOfJobCell.h"
#import "JobDetailViewController.h"
//#import "TConversationCellData.h"
#import "ChatViewController.h"

@class TConversationCellData;

@interface HRJobDetailViewController ()<UITableViewDelegate, UITableViewDataSource>
{
    BaseNetManager *netManager;
    NSMutableArray *itemDataArray;
    
    UITableView *jobTableView;
}
@end

@implementation HRJobDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    netManager = [BaseNetManager sharedManager];
    itemDataArray = [NSMutableArray arrayWithCapacity:0];
    
    [self initTopUI];
    [self loadData];
}
- (void)loadData {
    self.noDataView.hidden = YES;
    SVPHUDShow;
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:0];
    dic = [UIToolsControl getPublicParameter];
//    [dic setObject:self.infoModel.companyInfoModel.id forKey:@"company_id"];
    [dic setObject:self.infoModel.hrInfoModel.userid forKey:@"userid"];
    [dic setObject:@"1" forKey:@"page"];
    [dic setObject:PageSize forKey:@"number"];
    
    [BaseNetManager requestWithMethod:POST path:CompanyHrJobs_URL params:dic successBlock:^(NSDictionary * _Nonnull responseObject) {
        if ([responseObject[@"code"] integerValue] == 2000) {
            id data = responseObject[@"data"];
            for (NSDictionary *dic in data) {
                CompanyJobModel *model = [[CompanyJobModel alloc] init];
                [model setValuesForKeysWithDictionary:dic];
                [self->itemDataArray addObject:model];
            }
            [self initUI];
            SVPHUDDismiss;
        } else {
            SVPHUDError(responseObject[@"msg"]);
        }
    } failurBlock:^(NSError * _Nonnull error) {
        SVPHUDError(NetErrorAlertText);
        [self.noDataView vcNilStatusType:VCNilStatusTypeOfNoNet superView:self.view];
    }];
}
- (void)initTopUI {
    self.navigationItem.title = self.infoModel.hrInfoModel.name;
    
//    UIBarButtonItem *chat = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"chat_icon"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStyleDone target:self action:@selector(chatTap)];
//    self.navigationItem.rightBarButtonItem = chat;
    
    UILabel *nameLbl = [UIToolsControl initLabelWithFrame:CGRectMake(LeftSpace, 35*Scale, WIDTH-110*Scale, 25*Scale) text:[NSString stringWithFormat:@"%@·%@", self.infoModel.companyInfoModel.name, self.infoModel.hrInfoModel.position] textColor:THEMECOLOR_OF_TEXT_333333 font:Font_17];
    [self.view addSubview:nameLbl];
    
    UILabel *updateLbl = [UIToolsControl initLabelWithFrame:CGRectMake(StartX(nameLbl), EndY(nameLbl), 200, 20*Scale) text:self.infoModel.hrInfoModel.last_timeShow textColor:THEMECOLOR_OF_TEXT_9a9a9a font:Font_12];
    [self.view addSubview:updateLbl];
    
    UIImageView *imgView = [UIToolsControl initImageViewWithFrame:CGRectMake(WIDTH-81*Scale, 22*Scale, 66*Scale, 66*Scale) imageName:@"header_bg"];
    imgView.contentMode = UIViewContentModeScaleAspectFill;
    imgView.layer.cornerRadius = 33*Scale;
    imgView.layer.masksToBounds = YES;
    [self.view addSubview:imgView];
    if (self.infoModel.hrInfoModel.avatar && self.infoModel.hrInfoModel.avatar.length) {
        [imgView sd_setImageWithURL:[NSURL URLWithString:self.infoModel.hrInfoModel.avatar]];
    }
    UIView *lineView = [UIToolsControl initViewWithFrame:CGRectMake(0, EndY(updateLbl)+25*Scale, WIDTH, 10*Scale) backgroundColor:THEMECOLOR_OF_GRAY];
    [self.view addSubview:lineView];
    
    UILabel *titleLbl = [UIToolsControl initLabelWithFrame:CGRectMake(StartX(nameLbl), EndY(lineView)+15*Scale, 200, 45*Scale) text:@"ta发布过的职位" textColor:THEMECOLOR_OF_TEXT_333333 font:Font_15];
    [self.view addSubview:titleLbl];
}
- (void)initUI {
    jobTableView = [UIToolsControl initTableViewWithFrame:CGRectMake(0, 175*Scale, WIDTH, HEIGHT-HeightOfStatusBarAndNavigationBar-SIMEmojKeyBoardkBottomNOSafeArea_Height-175*Scale) target:self style:UITableViewStylePlain];
    jobTableView.separatorInset = UIEdgeInsetsMake(0, 15*Scale, 0, 15*Scale);
    jobTableView.rowHeight = 85*Scale;
    jobTableView.tableHeaderView = [[UIView alloc] init];
    jobTableView.tableFooterView = [[UIView alloc] init];
    [self.view addSubview:jobTableView];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (itemDataArray && itemDataArray.count) {
        return itemDataArray.count;
    }
    return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CompanyOfJobCell *cell = [tableView dequeueReusableCellWithIdentifier:isCellID];
    if (!cell) {
        cell = [[CompanyOfJobCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:isCellID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    CompanyJobModel *model = itemDataArray[indexPath.row];
    [cell reloadDataModel:model];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    CompanyJobModel *model = itemDataArray[indexPath.row];
    JobDetailViewController *JDVC = [[JobDetailViewController alloc] init];
    JDVC.id = model.id;
    [self.navigationController pushViewController:JDVC animated:YES];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.00001;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.00001;
}
- (void)chatTap {
    NSLog(@"chatTap");
    
    if (Signin) {
        [self pushChatVC];
    } else {
        __weak __typeof(self)weakSelf = self;
        [UIToolsControl presentSignin:self callBack:^(BOOL signinFlag) {
            __strong __typeof(weakSelf)strongSelf = weakSelf;
            [strongSelf pushChatVC];
        }];
    }
    
    
    
//    [self dismissViewControllerAnimated:NO completion:nil];
//    [((UITabBarController *)self.presentingViewController).selectedViewController pushViewController:chat animated:YES];
}
- (void)pushChatVC {
    TConversationCellData *data = [[TConversationCellData alloc] init];
    data.convId = self.infoModel.hrInfoModel.name;
    data.btpin_user_id = self.infoModel.hrInfoModel.userid;
    data.convType = TConv_Type_C2C;
    data.title = self.infoModel.hrInfoModel.name;
    ChatViewController *chat = [[ChatViewController alloc] init];
    chat.isNotice = NO;
    chat.isSessionPush = NO;
    chat.conversation = data;
    chat.job_id = @"";
    chat.job_cn = @"";
    chat.wage_cn = @"";
    chat.user_name = @"";
    [self.navigationController pushViewController:chat animated:YES];
}

@end
